﻿
@{
    Layout = null;
}
@model RoleModel;

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="~/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="~/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="~/css/site.css" media="all">
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-header">@(Model.RoleId>0?"修改角色":"添加角色")</div>
            <div class="layui-card-body" style="padding: 15px;">
                <form class="layui-form" lay-filter="form-group">
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="RoleName" lay-verify="title" autocomplete="off" placeholder="请输入角色名称" class="layui-input w300" value="@Model.RoleName">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="chkIsActive" lay-skin="switch" @Html.Raw(Model.IsActive == true ? " checked=\"checked\"" : "") lay-text="启用|禁用">
                        </div>
                    </div> 
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">描述</label>
                        <div class="layui-input-block">
                            <textarea name="Description" placeholder="请输入内容" class="layui-textarea w300">@Model.Description</textarea>
                        </div>
                    </div>
                    <div class="layui-form-item layui-layout-admin">
                        <div class="layui-input-block">
                            <div class="layui-footer" style="left: 0;">
                                <button class="layui-btn" lay-submit="" lay-filter="form-page">立即保存</button>
                                <a href="/Role/Index" class="layui-btn layui-btn-primary">返回列表</a>
                            </div>
                        </div>
                    </div>                    
                </form>
            </div>
        </div>
    </div>
    <script src="~/layuiadmin/layui/layui.js"></script>
    <script>
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form'], function () {
            var $ = layui.$
                , admin = layui.admin
                , element = layui.element
                , layer = layui.layer
                , form = layui.form;

            form.render(null, 'form-group');

            /* 自定义验证规则 */
            form.verify({
                title: function (value) {
                    if (value.length < 1) {
                        return '至少得1个字符啊';
                    }
                }              
            });

            ///* 监听提交 */
            form.on('submit(form-page)', function (data) {  
                data.field.IsActive = data.field.chkIsActive == 'on' ? true : false;
                console.log(data.field);

                $.ajax({                  
                    type: "post",
                    url: '/role/edit/@Model.RoleId',
                    data: data.field,
                    //async: false,
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1000) {
                            layer.alert(res.msg, function(index){
                              window.location.href = '/role/index';//do something
                              layer.close(index);
                            });
                        }
                        else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    }
                });
                //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                return false;
            });
        });
    </script>
</body>
</html>